use entity::{booking_status::*, meeting_room};
use sea_orm_migration::{prelude::*, sea_orm::Schema};

pub struct Migration;

impl MigrationName for Migration {
    fn name(&self) -> &str {
        "m20220520_123224_create_booking_status"
    }
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        let schema = Schema::new(manager.get_database_backend());

        let mut table = schema.create_table_from_entity(Entity);
        table
            .if_not_exists()
            .index(
                Index::create()
                    .col(Column::RoomId)
                    .col(Column::Day)
                    .unique(),
            )
            .foreign_key(
                ForeignKey::create()
                    .from(Entity, Column::RoomId)
                    .to(meeting_room::Entity, meeting_room::Column::Id)
                    .on_update(ForeignKeyAction::Cascade)
                    .on_update(ForeignKeyAction::Restrict),
            );
        manager.create_table(table).await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager.drop_table(Table::drop().table(Entity).to_owned()).await
    }
}
